from PySide2 import QtWidgets
import pyqtgraph as pg
import numpy as np

class MainWindow(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()
        self.setWindowTitle('数据可视化')
        #1 创建 PlotWidget 对象
        self.pw = pg.PlotWidget()
        # 2设置图表标题
        self.pw.setTitle("示例",color='008080',size='12pt')
        # 3设置上下左右的label
        self.pw.setLabel("left","Y坐标")
        self.pw.setLabel("bottom","X坐标")
        # 4背景色改为白色 默认为黑色
        self.pw.setBackground('w')
        xposion = [1,2,3,4,5,6,7,8,9,10]
        yposion= [0,2,4,2,3,1,9,2,5,5]
        # 5hour 和 temperature 分别是 : x, y 轴上的值
        self.pw.plot(xposion,
                     yposion,
                     pen=pg.mkPen('b') # 线条颜色
                    )

        self.pw.button1.clicked.connect(self.plot_line)


        # 创建其他Qt控件

        lineEdit1 = QtWidgets.QLabel('曲线一')
        lineEdit2 = QtWidgets.QLabel('X坐标')
        lineEdit3 = QtWidgets.QLabel('Y坐标')
        lineEdit4 = QtWidgets.QLabel('颜色')
        lineEdit5 = QtWidgets.QLabel('线形参数')
        lineEdit6 = QtWidgets.QLabel('标记字符')
        XpoinEdit=  QtWidgets.QPlainTextEdit()
        YpoinEdit = QtWidgets.QPlainTextEdit()
        Colorbox=   QtWidgets.QComboBox()
        Linebox =   QtWidgets.QComboBox()
        Markbox =   QtWidgets.QComboBox()
        lineEdit7 = QtWidgets.QLabel('横坐标标题')
        lineEdit8 = QtWidgets.QLabel('纵坐标标题')
        lineEdit9 = QtWidgets.QLabel('图表标题')
        XTextEdit = QtWidgets.QPlainTextEdit()
        YTextEdit = QtWidgets.QPlainTextEdit()
        TTextEdit = QtWidgets.QPlainTextEdit()



        button1=QtWidgets.QPushButton('确认')
        # button2 = QtWidgets.QPushButton('清除')



        #选项

        Colorbox.addItems(['蓝色', '黑色', '绿色', '黄色', '红色', '白色', '洋红色', '青绿色'])

        Linebox.addItems(['实线', '虚线', '点划线', '破折线'])
        Markbox.addItems(['无标记', '圆点', '圆圈', '向下三角形', '向上三角形', '向左三角形', '向右三角形', '五角星形', '加号', '下划线', 'x符号', '菱形'])

        #左侧交互区
        libox=QtWidgets.QGridLayout()#栅格
        libox.addWidget(lineEdit1,row=0,column=0)
        libox.addWidget(lineEdit2,row=2,column=1)
        libox.addWidget(XpoinEdit,row=2,column=2)
        libox.addWidget(lineEdit3,row=3,column=1)
        libox.addWidget(YpoinEdit, row=3, column=2)
        libox.addWidget(lineEdit4,row=4,column=1)
        libox.addWidget(Colorbox, row=4, column=2)
        libox.addWidget(lineEdit5,row=5,column=1)
        libox.addWidget(Linebox, row=5, column=2)
        libox.addWidget(lineEdit6,row=6,column=1)
        libox.addWidget(Markbox, row=6, column=2)
        libox.addWidget(lineEdit7, row=7, column=1)
        libox.addWidget(XTextEdit,row=7,column=2)
        libox.addWidget(lineEdit8, row=8, column=1)
        libox.addWidget(YTextEdit, row=8, column=2)
        libox.addWidget(lineEdit9, row=9, column=1)
        libox.addWidget(TTextEdit, row=9, column=2)

        libox.addWidget(button1,row=9,column=1)
        #libox.addWidget(button2, row=9, column=2)


        #右侧垂直
        lbox=QtWidgets.QHBoxLayout()#垂直
        lbox.addWidget(self.pw)
        #分左边交互端和右边图表，左边栅格 右边垂直 最后整体垂直
        # 垂直layout里面放 pyqtgraph图表控件 和 前面的水平layout
        vbox = QtWidgets.QHBoxLayout()#垂直
        vbox.addLayout(libox)
        vbox.addLayout(lbox)
        #按钮水平摆放然后和左边垂直 然后整体再和右边垂直

        # 设置全局layout
        self.setLayout(vbox)
    def plot_line(self):
        Line_Color = {'蓝色': 'b', '黑色': 'k', '绿色': 'g', '黄色': 'y', '红色': 'r', '白色': 'w', '洋红色': 'm',
                      '青绿色': 'c'}

        XpoinEdit1 = self.ui.XpoinEdit.toPlainText()#x坐标
        YpoinEdit1 = self.ui.YpoinEdit.toPlainText()#y坐标
        Colorbox1=self.ui.Colorbox.currentText()#颜色
        Linebox1 = self.ui.Linebox.currentText()#线形
        Markbox1 = self.ui.Markbox.currentText()#标记
        XTextEdit1 = self.ui.XTextEdit.toPlainText()  # x标题
        YTextEdit1 = self.ui.YTextEdit.toPlainText()  # y标题
        TTextEdit1 = self.ui.TTextEdit.toPlainText()  # 题目

        XpoinEditP = np.array(list(map(int, XpoinEdit1.split())))
        YpoinEditP = np.array(list(map(int, YpoinEdit1.split())))

        self.pw.setBackground('w')  # 默认背景色为黑改成白色

        if XpoinEditP and YpoinEditP!='':
            self.pw=pg.PlotWidget()
            self.pw.setTitle(TTextEdit1,size='12pt')
            self.pw.setLabel("left",YTextEdit1)
            self.pw.setLabel("bottom", XTextEdit1)
            self.pw.plot([1,2,3],[1,2,3],
                         pen=pg.mkPen(Line_Color[Colorbox1]))




if __name__ == '__main__':
    app = QtWidgets.QApplication()
    main = MainWindow()
    main.show()
    app.exec_()